package com.yk.framework.gui.core.impl;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Rectangle;
import java.util.List;

import javax.swing.JComponent;
import javax.swing.SwingUtilities;
import javax.swing.WindowConstants;

import com.yk.framework.gui.core.IYKWindow;
import com.yk.framework.gui.ctrl.MsgBox;
import com.yk.framework.gui.ctrl.YKFrame;
import com.yk.framework.gui.ctrl.YKLabel;
import com.yk.framework.gui.ctrl.YKPanel;
import com.yk.framework.gui.ctrl.YKStateBar;
import com.yk.framework.gui.ctrl.YKToolBar;
import com.yk.framework.gui.ctrl.YKToolButton;
import com.yk.platform.ui.UserEditPanel;

/**
 * @author lifetime
 * 
 */
@SuppressWarnings("serial")
public class NewFrame extends YKFrame implements IYKWindow {
	protected YKToolBar mainToolbar;
	protected YKPanel mainPanel;
	protected YKStateBar ykStateBar;
	protected CoreUI coreUI;
	protected YKLabel stateLabel;

	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				NewFrame inst = new NewFrame(new UserEditPanel());
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
	}

	public NewFrame(CoreUI coreUI) {
		super();
		this.coreUI = coreUI;
		setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
		initGUI();
	}

	protected void initGUI() {
		initToolbar();
		initStatusBar();
		getContentPane().add(getMainPanel(), BorderLayout.CENTER);
		this.setSize(coreUI.getSize().width, coreUI.getSize().height + 20);
		pack();
	}

	protected void initStatusBar() {
		ykStateBar = coreUI.getStateBar();
		if (ykStateBar == null) {
			ykStateBar = new YKStateBar();
		}
		initykStateBar();
		getContentPane().add(ykStateBar, BorderLayout.SOUTH);
		ykStateBar.setPreferredSize(new java.awt.Dimension(coreUI.getSize().width, 21));
	}

	protected void initToolbar() {
		mainToolbar = new YKToolBar();
		changeUIToolBar(coreUI);
		getContentPane().add(mainToolbar, BorderLayout.NORTH);
		mainToolbar.setPreferredSize(new java.awt.Dimension(coreUI.getSize().width, 28));
	}

	protected JComponent getMainPanel() {
		mainPanel = new YKPanel();
		mainPanel.setLayout(new BorderLayout());
		mainPanel.add(coreUI, BorderLayout.CENTER);
		return mainPanel;
	}

	public void changeUIToolBar(CoreUI ui) {
		List<YKToolButton> buttons = ui.getToolButtons();
		for (int i = 0; i < buttons.size(); i++) {
			mainToolbar.add(buttons.get(i));
		}
		mainToolbar.validate();
		mainToolbar.doLayout();
	}

	protected void initykStateBar() {
		ykStateBar.setLayout(new FlowLayout(FlowLayout.LEFT, 0, 0));
		stateLabel = new YKLabel();
		stateLabel.setPreferredSize(new Dimension(150, 20));
		stateLabel.setBounds(new Rectangle(0, 0, 150, 20));
		ykStateBar.add(stateLabel);
	}

	public void showStatusMessage(final String msg) {
		if (ykStateBar == null) {
			MsgBox.showInfo(this, msg);
			return;
		}
		final Color col = new Color(123, 132, 232);
		stateLabel.setText(msg);
		final Color oldCr = stateLabel.getForeground();
		Thread t = new Thread(new Runnable() {
			public void run() {
				for (int i = 0; i < 3; i++) {
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							stateLabel.setText(msg);
							stateLabel.setForeground(col);
						}
					});
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					SwingUtilities.invokeLater(new Runnable() {
						public void run() {
							stateLabel.setText(null);
							stateLabel.setForeground(oldCr);
						}
					});
					try {
						Thread.sleep(200);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						stateLabel.setText(msg);
					}
				});
				try {
					Thread.sleep(2000);
				} catch (InterruptedException e) {
					e.printStackTrace();
				}
				SwingUtilities.invokeLater(new Runnable() {
					public void run() {
						stateLabel.setText(null);
					}
				});
			}
		});
		t.start();
	}

	public YKToolBar getMainToolbar() {
		return mainToolbar;
	}

	public void setMainToolbar(YKToolBar mainToolbar) {
		this.mainToolbar = mainToolbar;
	}

	public YKStateBar getYkStateBar() {
		return ykStateBar;
	}

	public void setYkStateBar(YKStateBar ykStateBar) {
		this.ykStateBar = ykStateBar;
	}

}
